package Constructions.EnemyConstructions;

import java.awt.Graphics;
import java.util.ArrayList;
import java.util.ConcurrentModificationException;

import Main.SpaceGame;

public class EnemyConstructionManager 
{
	SpaceGame main;
	public ArrayList<EnemyConstruction> constructionList = new ArrayList<EnemyConstruction>();
	
	public EnemyConstructionManager(SpaceGame main)
	{
		this.main=main;
	}
	
	public void draw(Graphics g)
	{
		for (int i = 0;i<constructionList.size();i++)
		{
			constructionList.get(i).draw(g);
		}
	}
	
	public void add(EnemyConstruction c)
	{
		constructionList.add(c);
		for (int i = c.getX();i<c.getX()+c.getWidth();i++)
		{
			for (int j = c.getY();j<c.getY()+c.getHeight();j++)
			{
				main.world.tiles[i][j].setHasConstruction(true);
			}
		}
	}
	
	public void remove(int x,int y)
	{
		try
		{
			for (EnemyConstruction c:constructionList)
			{
				if (c.getX() == x && c.getY() == y)
				{
					constructionList.remove(c);
					for (int i = c.getX();i<c.getX()+c.width;i++)
					{
						for (int j = c.getY();j<c.getY()+c.height;j++)
						{
							main.world.tiles[i][j].setHasConstruction(false);
						}
					}
				}
			}
		}
		catch(ConcurrentModificationException e)
		{
			
		}
	}
	
	public void update()
	{
		for(EnemyConstruction c:constructionList)
		{
			c.update();
			c.text.clear();
			c.colors.clear();
			c.setText();
		}
	}
	
	public EnemyConstruction getConstruction(int x,int y)
	{
		EnemyConstruction a = null;
		for(EnemyConstruction c:constructionList)
		{
			if (c.getX() == x && c.getY() == y)
				a=c;
		}
		return a;
	}
}
